package com.yonyou.rabbitmq.service;

import com.rabbitmq.client.Channel;
import com.yonyou.rabbitmq.common.MessageConstants;
import com.yonyou.rabbitmq.utils.MessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;

@Slf4j
public abstract class BaseConsumerService {

    public void process(Channel channel, Message message) throws Exception {
        final long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String tenantId = (String)message.getMessageProperties().getHeaders().get(MessageConstants.TENANT_ID);
        log.info("站点,deliveryTag:{}",tenantId,deliveryTag);
        String msg = MessageUtils.getBodyStr(message);
        log.info("站点,messageBody:{}",tenantId,msg);
        try {
            Thread.sleep(2000);
            doBusiness(message,tenantId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("{}站点,消费消息异常",tenantId,e);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false, false);
            throw new AmqpException("消费消息异常");
        }
    }

    public abstract void doBusiness(Message message,String tenantId);
}
